home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 48 / Mac Magazin CD 48.iso / Software / Entwickler / Drop UNIX 1.3 / DropUNIX Library ƒ / Lib Sources / DSAppleEvents.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-22  |  8.0 KB  |  252 lines  |  [TEXT/CWIE]

  1. /******************************************************************************
  2. **
  3. **  Project Name:    DropShell
  4. **     File Name:    DSAppleEvents.c
  5. **
  6. **   Description:    Generic AppleEvent handling routines
  7. **                    
  8. **                    This is the set of routines for handling the required Apple events.
  9. **                    You should NEVER have to modify this file!!!
  10. **                    Simply add code in DSUserProcs to the routines called by these.
  11. **
  12. *******************************************************************************
  13. **                       A U T H O R   I D E N T I T Y
  14. *******************************************************************************
  15. **
  16. **    Initials    Name
  17. **    --------    -----------------------------------------------
  18. **    LDR            Leonard Rosenthol
  19. **    MTC            Marshall Clow
  20. **    SCS            Stephan Somogyi
  21. **
  22. *******************************************************************************
  23. **                      R E V I S I O N   H I S T O R Y
  24. *******************************************************************************
  25. **
  26. **      Date        Author    Description
  27. **    ---------    ------    ---------------------------------------------
  28. **    20 Feb 94    LDR        Modified _HandleDocs to pass item count to preflight & postflight
  29. **    11 Dec 93    SCS        Universal Headers/UPPs (Phoenix 68k/PPC & PPCC)
  30. **                        Skipped System 6 compatible rev of DropShell source
  31. **    11/24/91    LDR        Added a handler for 'pdoc' as per DTS recommendation
  32. **                            This caused some reorg & userProc routine changes
  33. **                            I also created a new common AEVT doc extractor
  34. **                        Cleaned up error handling by adding FailErr
  35. **                        Cleaned up the placement of braces
  36. **                        Added the passing of a userDataHandle to the odoc/pdoc routines
  37. **    10/29/91    SCS        Changes for THINK C 5
  38. **    10/28/91    LDR        Officially renamed DropShell (from QuickShell)
  39. **                        Added a bunch of comments for clarification
  40. **    10/06/91    MTC        Converted to MPW C
  41. **    04/09/91    LDR        Added to Projector
  42. **
  43. ******************************************************************************/
  44.  
  45. #include "DSGlobals.h"
  46. #include "DSUtils.h"
  47. #include "DSUserProcs.h"
  48.  
  49. #include "DSAppleEvents.h"
  50.  
  51. AEEventHandlerUPP    OAPPHandlerUPP, ODOCHandlerUPP, PDOCHandlerUPP, QUITHandlerUPP;
  52.  
  53. /*
  54.     This routine does all initialization for AEM, including the
  55.     creation and then population of the dispatch table.
  56. */
  57.  
  58. pascal void InitAEVTStuff ()  {
  59.     OSErr aevtErr;
  60.  
  61.     aevtErr = noErr;
  62.     
  63.     if ( aevtErr == noErr )    {
  64.         OAPPHandlerUPP = NewAEEventHandlerProc(HandleOAPP);
  65.         aevtErr = AEInstallEventHandler ( kCoreEventClass, kAEOpenApplication, OAPPHandlerUPP, 0, false );
  66.     }
  67.  
  68.  
  69.     if ( aevtErr == noErr )    {
  70.         ODOCHandlerUPP = NewAEEventHandlerProc(HandleODOC);
  71.         aevtErr = AEInstallEventHandler ( kCoreEventClass, kAEOpenDocuments, ODOCHandlerUPP, 0, false );
  72.     }
  73.  
  74.     if ( aevtErr == noErr )    {
  75.         PDOCHandlerUPP = NewAEEventHandlerProc(HandlePDOC);
  76.         aevtErr = AEInstallEventHandler ( kCoreEventClass, kAEPrintDocuments, PDOCHandlerUPP, 0, false );
  77.     }
  78.  
  79.     if ( aevtErr == noErr )    {
  80.         QUITHandlerUPP = NewAEEventHandlerProc(HandleQuit);
  81.         aevtErr = AEInstallEventHandler ( kCoreEventClass, kAEQuitApplication, QUITHandlerUPP, 0, false );
  82.     }
  83.  
  84.     if ( aevtErr == noErr )
  85.         InstallOtherEvents ();
  86.  
  87.         
  88.     if ( aevtErr != noErr )
  89.         ;        // report an error if you are so included
  90. }
  91.  
  92.  
  93.  
  94. /*    
  95.     This routine is a utility routine for checking that all required 
  96.     parameters in the Apple event have been used.
  97. */
  98.  
  99. OSErr GotRequiredParams ( AppleEvent *theAppleEvent ) {
  100.     DescType    typeCode;
  101.     Size        actualSize;
  102.     OSErr        retErr, err;
  103.  
  104.     err = AEGetAttributePtr ( theAppleEvent, keyMissedKeywordAttr,
  105.                     typeWildCard, &typeCode, NULL, 0, &actualSize );
  106.     
  107.     if ( err == errAEDescNotFound )    // we got all the required params: all is ok
  108.         retErr = noErr;
  109.     else if ( err == noErr )
  110.         retErr = errAEEventNotHandled;
  111.     else 
  112.         retErr = err;
  113.     
  114.     return retErr;
  115. }
  116.  
  117. /*    
  118.     This routine is the handler for the oapp (Open Application) event.
  119.     
  120.     It first checks the number of parameters to make sure we got them all 
  121.     (even though we don't want any) and then calls the OpenApp userProc in QSUserProcs.
  122.     Finally it checks to see if the caller wanted a reply & sends one, setting any error.
  123. */
  124.  
  125. pascal OSErr HandleOAPP ( AppleEvent *theAppleEvent, AppleEvent *reply, long /*handlerRefcon*/ ) {
  126.  
  127.     OSErr err;
  128.  
  129.     FailOnError(err = GotRequiredParams ( theAppleEvent ), kAEVTErr);
  130.  
  131.     // let's show the user the splash screen
  132. //    ShowWindow(gSplashScreen);
  133.  
  134.     OpenApp ();        // pass it on to the app specific routine
  135.  
  136.     if ( reply->dataHandle != NULL )    /*    a reply is sought */
  137.         FailOnError(err = AEPutParamPtr ( reply, 'errs', 'TEXT', "Opening", 7 ), kAEVTErr);
  138.     
  139.     return err;
  140. }
  141.  
  142.  
  143. /*    
  144.     This routine is the handler for the quit (Quit Application) event.
  145.     
  146.     It first checks the number of parameters to make sure we got them all 
  147.     (even though we don't want any) and then calls the QuitApp userProc in QSUserProcs.
  148.     Finally it checks to see if the caller wanted a reply & sends one, setting any error.
  149. */
  150.  
  151.  
  152. pascal OSErr HandleQuit ( AppleEvent *theAppleEvent, AppleEvent *reply, long /*handlerRefcon*/ ) {
  153.  
  154.     OSErr err;
  155.     
  156.     FailOnError( err = GotRequiredParams ( theAppleEvent ), kAEVTErr);
  157.  
  158.     QuitApp ();        // pass it on to the app specific routine
  159.  
  160.     if ( reply->dataHandle != NULL )    /*    a reply is sought */
  161.         FailOnError(err = AEPutParamPtr ( reply, 'errs', 'TEXT', "Qutting", 7 ), kAEVTErr);
  162.     
  163.     return err;
  164. }
  165.  
  166.  
  167. /*    
  168.     This routine is the low level processing routine for both the 
  169.     odoc (Open Document) and pdoc (Print Document) events.
  170.     
  171.     This routine is the key one, since this is how we get the list of
  172.     files/folders/disks to process.  The first thing to do is the get the
  173.     list of files, and then make sure that's all the parameters (should be!).
  174.     We then send call the PreflightDocs routine (from DSUserProcs), process
  175.     each file in the list by calling OpenDoc (again in DSUserProcs), and finally
  176.     call PostflightDocs (you know where) and setting a return value.
  177. */
  178.  
  179. pascal OSErr _HandleDocs ( AppleEvent *theAppleEvent, AppleEvent * /*reply*/, Boolean opening ) {
  180.  
  181.  
  182.     OSErr        err;
  183.     FSSpec        myFSS;
  184.     AEDescList    docList;
  185.     long        index, itemsInList;
  186.     Size        actualSize;
  187.     AEKeyword    keywd;
  188.     DescType    typeCode;
  189.     Handle        userDataHandle = NULL;
  190.  
  191.     FailOnError(err = AEGetParamDesc ( theAppleEvent, keyDirectObject, typeAEList, &docList ), kAEVTErr);
  192.     FailOnError(err = GotRequiredParams ( theAppleEvent ), kAEVTErr);
  193.  
  194.     /*    How many items do we have?. */
  195.     /* NOTE: Moved here in DS 2.0 due to requests for this info in preflighter */
  196.     FailOnError(err = AECountItems ( &docList, &itemsInList ), kAEVTErr);
  197.     
  198.     if (PreFlightDocs (opening, itemsInList, &userDataHandle))    {    // let the app do any preflighting it might need
  199.  
  200.         for ( index = 1; index <= itemsInList; index++ ) {
  201.             FailOnError(err = AEGetNthPtr ( &docList, index, typeFSS, &keywd, &typeCode,
  202.                     (Ptr) &myFSS, sizeof ( myFSS ), &actualSize ), kAEVTErr);
  203.     
  204.             OpenDoc( &myFSS, opening, userDataHandle );    // call the userProc
  205.         }
  206.     
  207.         PostFlightDocs (opening, itemsInList, userDataHandle);    // cleanup time
  208.     }
  209.     else
  210.         err = errAEEventNotHandled;    // tells AEM that we didn't handle it!
  211.         
  212.     FailOnError(AEDisposeDesc ( &docList ), kAEVTErr);
  213.  
  214.     return err;
  215. }
  216.  
  217. /*
  218.     This routine is the handler for the odoc (Open Document) event.
  219.     
  220.     The odoc event simply calls the common _HandleDocs routines, which will
  221.     do the dirty work of parsing the AEVT & calling the userProcs.
  222. */
  223.  
  224. pascal OSErr HandleODOC ( AppleEvent *theAppleEvent, AppleEvent *reply, long /* handlerRefcon */ ) {
  225.  
  226.     
  227.     return (_HandleDocs(theAppleEvent, reply, true));    // call the low level routine
  228. }
  229.  
  230. /*
  231.     This routine is the handler for the pdoc (Print Document) event.
  232.     
  233.     The pdoc event like the odoc simply calls the common _HandleDocs routines
  234. */
  235.  
  236. pascal OSErr HandlePDOC ( AppleEvent *theAppleEvent, AppleEvent *reply, long /* handlerRefcon */ ) {
  237.  
  238.     
  239.     return (_HandleDocs(theAppleEvent, reply, false));    // call the low level routine
  240. }
  241.  
  242. /*    
  243.     This is the routine called by the main event loop, when a high level
  244.     event is found.  Since we only deal with Apple events, and not other
  245.     high level events, we just pass everything onto the AEM via AEProcessAppleEvent
  246. */
  247.  
  248. pascal void DoHighLevelEvent ( EventRecord *event ) {
  249.  
  250.     FailOnError ( AEProcessAppleEvent ( event ), kAEVTErr);
  251. }
  252.